﻿using Propro.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;
using WebMatrix.WebData;

namespace Propro
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
            if (!Roles.RoleExists("Administration"))
            {
                Roles.CreateRole("Administration");
            }
            if (Membership.GetUser("Administrator", false) == null)
            {
                WebSecurity.CreateUserAndAccount("Administrator", "Test123");
            }
            if (!Roles.GetRolesForUser("Administrator").Contains("Administration"))
            {
                Roles.AddUsersToRoles(new[] { "Administrator" }, new[] { "Administration" });
            }

            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError();

            LogWriter.LoggerWriter.log(Enum.LogSoort.error, ex.Message);
        }

        protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            var handler = Context.Handler as MvcHandler;
            var routeData = handler != null ? handler.RequestContext.RouteData : null;
            var routeCulture = routeData != null ? routeData.Values["culture"].ToString() : null;
            var languageCookie = HttpContext.Current.Request.Cookies["lang"];
            var userLanguages = HttpContext.Current.Request.UserLanguages;

            // Set the Culture based on a route, a cookie or the browser settings,
            // or default value if something went wrong
            //if (routeCulture != "nl-BE" || routeCulture != "en-US")
            //{
            //    routeCulture = "en-US";
            //}

            CultureInfo cultureInfo;
            try
            {
                cultureInfo = new CultureInfo(
                    routeCulture ?? (languageCookie != null
                       ? languageCookie.Value
                       : userLanguages != null
                           ? userLanguages[0]
                           : "en-US")
                );
            }
            catch (CultureNotFoundException)
            {
                cultureInfo = new CultureInfo("en");
            }

            Thread.CurrentThread.CurrentUICulture = cultureInfo;
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
        }
    }
}